home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / debug.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  1.7 KB  |  58 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2008, 2010 Red Hat, Inc.
  4. ## Authors:
  5. ##  Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import sys
  22. import traceback
  23.  
  24. _debug=False
  25. def debugprint (x):
  26.     if _debug:
  27.         try:
  28.             sys.stderr.write (x + "\n")
  29.             sys.stderr.flush ()
  30.         except:
  31.             pass
  32.  
  33. def get_debugging ():
  34.     return _debug
  35.  
  36. def set_debugging (d):
  37.     global _debug
  38.     _debug = d
  39.  
  40. def fatalException (exitcode=1):
  41.     nonfatalException (type="fatal", end="Exiting")
  42.     sys.exit (exitcode)
  43.  
  44. def nonfatalException (type="non-fatal", end="Continuing anyway.."):
  45.     d = get_debugging ()
  46.     set_debugging (True)
  47.     debugprint ("Caught %s exception.  Traceback:" % type)
  48.     (type, value, tb) = sys.exc_info ()
  49.     tblast = traceback.extract_tb (tb, limit=None)
  50.     if len (tblast):
  51.         tblast = tblast[:len (tblast) - 1]
  52.     extxt = traceback.format_exception_only (type, value)
  53.     for line in traceback.format_tb(tb):
  54.         debugprint (line.strip ())
  55.     debugprint (extxt[0].strip ())
  56.     debugprint (end)
  57.     set_debugging (d)
  58.